package volkov.ja;

import org.openqa.selenium.By;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import volkov.ja.pages.TestBase;
import volkov.ja.util.MyProjectException;
import volkov.ja.util.TestLogger;

public class MovieCreateNegativeTest1 extends TestBase {
	private static TestLogger LOG;

	@BeforeClass
	public void prepareTest() {
		LOG = new TestLogger(MovieCreateNegativeTest1.class, getSessionId());
		LOG.info("Prepare environment: loading database fixtures");
		db.prepareFixture("default");
		loginTestApplication();
	}

	@Test
	public void testMovieCreateNegative1() throws Exception {
		String title = "The movie and an Absent Year";
		LOG.info("Go to the movies list");
		driver.get(baseUrl + "#!/sort/name%20asc/");
		LOG.info("Click on the \"add movie\" button");
		clickIfPresent(By.cssSelector("img[alt=\"Add movie\"]"));
		LOG.info("Fill only the name for a new movie");
		driver.findElement(By.name("name")).clear();
		driver.findElement(By.name("name")).sendKeys(title);
		LOG.info("Submitting data for the new movie");
		driver.findElement(By.id("submit")).click();
		// check results
		LOG.info("Check that the movie wasn't created");
		if (!getMoviesFromDb(title).isEmpty()) {
			throw new MyProjectException(
					"The movie is \""
							+ title
							+ "\" is created in the database possibly with an invalid year");
		}
		LOG.info("Check that the movie name is filled");
		checkElementWithContent(
				By.xpath("//form[@id='updateform']//input[@name='name']"),
				title,
				"The name of the movie is empty after saving attempt. Should be \""
						+ title + "\"");
		LOG.info("Check that the error message is shown");
		checkElementWithContent(
				By.xpath("//form[@id='updateform']/table//label[@class='error'][@for='year']"),
				"This field is required",
				"Error message for absent attribute \"year\" not found");
	}

}
